package org.andao.commons.service;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import org.andao.core.utils.ExStringUtils;


/**
 * 天气预报服务 
 * 
 * @author Marco.hu(hzg139@163.com) 
 */
public class WeatherReportService {

	/**默认城市天气*/
	public final static String DEFAULT_CITY = "广州";
	
	/**
	 * 获取城市的天气情况
	 * @param city 城市拼音
	 * @return
	 */
	public static String getCityWeather(String city){		
		InputStream inputstream = null;
		InputStreamReader in = null;
		StringBuilder bd = new StringBuilder();
		try {
			if(ExStringUtils.isEmpty(city)){
				city = DEFAULT_CITY;
			}
			
			URL url = new URL("http://weather.china.xappengine.com/api?city="+city);
			inputstream = url.openStream(); 	
		    in =  new InputStreamReader(inputstream,"UTF-8");
		    BufferedReader bfReader = new BufferedReader(in);
		    
		    String line = null;
		     while ((line = bfReader.readLine()) != null) {
		    	 bd.append(line);
		     }	  		  
			
		} catch (Exception e) {
			
		}finally{
			try {
				if(null!=inputstream){
					inputstream.close();
				}
				if(null!=in){
					in.close();
				}
			} catch (IOException e) {				
			}
			
		}
	
		return bd.toString();
	}
	
}
